﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using KishOnlineCommone.CarDTOs;
using KishOnlineDomain.Controller;
using KishOnlineCommone;
using KishOnlineDomain.Common;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CarAnn
{

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public List<BrandDTO> GetAllBrands()
    {
        // Add your operation implementation here
        return new CarControllers().CRUDBrand.GetAll();
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public List<ModelDTO> GetModel(int modelID)
    {
        // Add your operation implementation here
        return new CarControllers().CRUDModel.GetBaseOnProprety("BrandID", modelID);
    }


    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public List<ColorDTO> GetColor()
    {
        // Add your operation implementation here
        return new CarControllers().CRUDColor.GetAll();
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public PagingDTO<CarAnnouncementDTO> GetAllUserCarAnn(int Start, int End,bool WithTotal)
    {
        // Add your operation implementation here
        return new CarControllers().CarAnnouncement.GetAnnouncementsOfCurrentUser(Start, End, WithTotal);
    }
    // Add more operations here and mark them with [OperationContract]

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public List<CarStatusDTO> GetAllStatus()
    {
        // Add your operation implementation here
        return new CarControllers().CRUDCarStats.GetAll();
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO AddCarAnn(CarAnnouncementDTO CarAnn)
    {
        // Add your operation implementation here
        if (UserController.GetCurrentUser() != null)
        {
            if (CarAnn.CarID > 0)
            {
                CarAnn.Car.ID = CarAnn.CarID;
                return new CarControllers().CarAnnouncement.Update(CarAnn);
            }
            else
            {
                var Result = new CarControllers().CarAnnouncement.Add(CarAnn);
                return new ResultDTO { IsSucceed = Result.IsSucceed, Message = Result.Message };
            }
        }
        else
        {
            return new ResultDTO { IsSucceed = false, Message = MessageReader.Get(MessageType.Error, 50) };
        }

    }


    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO<CarAnnouncementDTO> GetCarAnn(int ID)
    {
        // Add your operation implementation here        
        var data = new CarControllers().CarAnnouncement.Get(ID);
        return new ResultDTO<CarAnnouncementDTO> { IsSucceed = true, Data = data, Message = null };
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO DelCarAnn(int ID)
    {
        // Add your operation implementation here        
        return new CarControllers().CarAnnouncement.Delete(ID);
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public PagingDTO<CarDTO> SerachCar(SerachCarDTO Condition, int Start, int End, bool withTotal, CarSortFeild? SearchField, bool IsAscend)
    {
        // Add your operation implementation here      
        var result = new CarControllers().CRUDCar.SerachCar(Condition, Start, End, withTotal, SearchField, IsAscend);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public PagingDTO<CarDTO> SearchAllCar(string txt, int Start, int End, bool withTotal, bool SortByStartDate, bool IsAscend)
    {
        // Add your operation implementation here      
        var result = new CarControllers().SearchAllCar(txt, Start, End, withTotal, SortByStartDate, IsAscend);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public PagingDTO<CarStoreDTO> SearchAllCarStore(string txt, int Start, int End, bool withTotal, bool SortByStartDate, bool IsAscend)
    {
        // Add your operation implementation here      
        var result = new CarControllers().SerachALLCarStore(txt, Start, End, withTotal, SortByStartDate, IsAscend);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public PagingDTO<ServiceCenterDTO> SearchAllServiceCenter(string txt, int Start, int End, bool withTotal, bool SortByStartDate, bool IsAscend)
    {
        // Add your operation implementation here      
        var result = new CarControllers().SerachALLService(txt, Start, End, withTotal, SortByStartDate, IsAscend);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public AllRelatedEntityDTO GetTopAll(int Count)
    {
        // Add your operation implementation here      
        var result = new CarControllers().GetTopEntity(Count);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public CarDetailDTO GetCarDetilas(int CarID)
    {
        // Add your operation implementation here  
        CarControllers carController = new CarControllers();
        CarDTO car = carController.CRUDCar.Get(CarID);
        var Related = carController.GetRelatedEntity(car);
        if (Related.Car != null)
        {
            Related.Car.RemoveAll(q => q.ID == car.ID);
        }
        UserDTO CarOwner = carController.CRUDCar.GetOwnerOfCar(CarID);
        return new CarDetailDTO
        {
            Car = car,
            Related = Related,
            Owner = CarOwner
        };
    }
    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public PagingDTO<InActiveCarsDTO> GetInActiveCar(string Owner, string carModel, int Start, int End, bool WithTotal)
    {
        // Add your operation implementation here
        return new CarControllers().GetInActiveAnnouncement(Owner, carModel, Start, End, WithTotal);
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO ActivatCar(int CarID, bool IsInCarStore)
    {
        // Add your operation implementation here
        if (IsInCarStore)
        {
            return new CarControllers().CarSotre.ActivateCarInCarStore(CarID);
        }
        else
        {
            return new CarControllers().CarAnnouncement.ActivateAnnouncement(CarID);
        }
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO ActivatCars(List<ApproveAnn> CarsApprove)
    {
        // Add your operation implementation here
        var CarStoreApp = CarsApprove.Where(q => q.IsInCarStore).Select(q => q.carID).ToList();
        var CarAnnApp = CarsApprove.Where(q => !q.IsInCarStore).Select(q => q.carID).ToList();
        CarControllers contoller = new CarControllers();
        ResultDTO CarStoreResult = contoller.CarSotre.ActivateCarsInCarStore(CarStoreApp);
        ResultDTO CarAnntoreResult = contoller.CarAnnouncement.ActivateAnnouncements(CarAnnApp);
        return CarAnntoreResult;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public CarDTO GetCar(int CarID)
    {
        // Add your operation implementation here  
        CarControllers carController = new CarControllers();
        CarDTO car = carController.CRUDCar.Get(CarID);
        return car;
    }
    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO DelCarBrand(int ID)
    {
        // Add your operation implementation here  
        var result = new CarControllers().CRUDBrand.Delete(ID);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO UpdateCarBrand(BrandDTO DTO)
    {
        // Add your operation implementation here  
        var result = new CarControllers().CRUDBrand.Update(DTO);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO<int> AddCarBrand(BrandDTO DTO)
    {
        // Add your operation implementation here  
        var result = new CarControllers().CRUDBrand.Add(DTO);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO DelCarColor(int ID)
    {
        // Add your operation implementation here  
        var result = new CarControllers().CRUDColor.Delete(ID);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO UpdateCarColor(ColorDTO DTO)
    {
        // Add your operation implementation here  
        var result = new CarControllers().CRUDColor.Update(DTO);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO<int> AddCarColor(ColorDTO DTO)
    {
        // Add your operation implementation here  
        var result = new CarControllers().CRUDColor.Add(DTO);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO DelCarStatus(int ID)
    {
        // Add your operation implementation here  
        var result = new CarControllers().CRUDCarStats.Delete(ID);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO UpdateCarStatus(CarStatusDTO DTO)
    {
        // Add your operation implementation here  
        var result = new CarControllers().CRUDCarStats.Update(DTO);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO<int> AddCarStatus(CarStatusDTO DTO)
    {
        // Add your operation implementation here  
        var result = new CarControllers().CRUDCarStats.Add(DTO);
        return result;
    }


    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO DelCarModel(int ID)
    {
        // Add your operation implementation here  
        var result = new CarControllers().CRUDModel.Delete(ID);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO UpdateCarModel(ModelDTO model)
    {
        // Add your operation implementation here  
        var result = new CarControllers().CRUDModel.Update(model);
        return result;
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO<int> AddCarModel(ModelDTO model)
    {
        // Add your operation implementation here  
        var result = new CarControllers().CRUDModel.Add(model);
        return result;
    }


    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public PagingDTO<CarStoreDTO> GetInActiveCarStore(string StoreName, int Start, int End, bool WithTotal)
    {
        // Add your operation implementation here
        return new CarControllers().CarSotre.GetInActiveStores(StoreName, Start, End, WithTotal);
    }
    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO ActivatCarStore(int CarStoreID)
    {
        // Add your operation implementation here
        return new CarControllers().CarSotre.ActivateCarStore(CarStoreID);
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO ActivatCarStores(List<int> CarStoreIDs)
    {
        // Add your operation implementation here  
        return new CarControllers().CarSotre.ActivateCarStores(CarStoreIDs);
    }
    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public PagingDTO<CarStoreDTO> GetCarStoresOfCurrentUser(int Start, int End, bool WithTotal)
    {
        // Add your operation implementation here
        return new CarControllers().CarSotre.GetCarStoresOfCurrentUser(Start, End, WithTotal);
    }
    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public CarStoreDTO GetCarStore(int carStoreID)
    {
        // Add your operation implementation here
        return new CarControllers().CarSotre.Get(carStoreID);
    }
    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public PagingDTO<CarDTO> GetCarOfCarStore(int carStoreID, int Start, int End, bool WithTotal)
    {
        // Add your operation implementation here
        return new CarControllers().CarSotre.GetCarInCarStore(carStoreID, Start, End, WithTotal);
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public List<ProvinceDTO> GetAllProvince()
    {
        // Add your operation implementation here
        return new CarControllers().CRUDProvince.GetAll();
    }
    
    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO UpdateCarStore(CarStoreDTO CarStore)
    {
        if (UserController.GetCurrentUser() != null)
        {
            // Add your operation implementation here
            if (CarStore.ID > 0)
            {
                CarStoreDTO OldCarStore = new CarControllers().CarSotre.Get(CarStore.ID);
                if (OldCarStore.UserID == UserController.GetCurrentUser().ID)
                {
                    CarStore.CarStoreTariffID = OldCarStore.CarStoreTariffID;
                    return new CarControllers().CarSotre.Update(CarStore);
                }
                else
                {
                    return new ResultDTO { IsSucceed = false, Message = MessageReader.Get(MessageType.Error, 2) };
                }
            }
            else
            {
                var result = new CarControllers().CarSotre.Add(CarStore);
                return new ResultDTO { IsSucceed = result.IsSucceed, Message = result.Message };
            }
        }
        else
        {
            return new ResultDTO { IsSucceed = false, Message = MessageReader.Get(MessageType.Error, 50) };
        }
    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO AddCarInCarStore(int CarStoreID,CarDTO Car)
    {
        // Add your operation implementation here
        if (UserController.GetCurrentUser() != null)
        {
            if (Car.ID > 0)
            {
                return new CarControllers().CarSotre.UpdateCarInCarStore(CarStoreID,Car);
            }
            else
            {
                var Result = new CarControllers().CarSotre.AddCarToCarStore(CarStoreID, Car);
                return new ResultDTO { IsSucceed = Result.IsSucceed, Message = Result.Message };
            }
        }
        else
        {
            return new ResultDTO { IsSucceed = false, Message = MessageReader.Get(MessageType.Error, 50) };
        }

    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public CarDTO GetCarInCarStore(int CarStoreID, int CarID)
    {
        // Add your operation implementation here
        if (UserController.GetCurrentUser() != null)
        {
            if (CarID > 0)
            {
                return new CarControllers().CarSotre.GetCarInCarStore(CarStoreID,CarID);
            }
            else
            {
                return null;
                //return new ResultDTO { IsSucceed = false, Message = null };
            }
        }
        else
        {
            return null;
            //return new ResultDTO { IsSucceed = false, Message = MessageReader.Get(MessageType.Error, 50) };
        }

    }
    
    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO DeleteCarInCarStore(int CarStoreID, int CarID)
    {
        // Add your operation implementation here
        if (UserController.GetCurrentUser() != null)
        {
            if (CarID > 0)
            {
                return new CarControllers().CarSotre.DeleteCarInCarStore(CarStoreID,CarID);
            }
            else
            {
                //return null;
                return new ResultDTO { IsSucceed = false, Message = null };
            }
        }
        else
        {
            
            return new ResultDTO { IsSucceed = false, Message = MessageReader.Get(MessageType.Error, 50) };
        }

    }

    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    public ResultDTO DeleteCarStore(int CarStoreID)
    {
        // Add your operation implementation here
        if (UserController.GetCurrentUser() != null)
        {
            if (CarStoreID > 0)
            {
                return new CarControllers().CarSotre.Delete(CarStoreID);
            }
            else
            {
                return new ResultDTO { IsSucceed = false, Message = null };
            }
        }
        else
        {

            return new ResultDTO { IsSucceed = false, Message = MessageReader.Get(MessageType.Error, 50) };
        }

    }
}
